<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1, user-scalable=no">
  <meta name="description" content="API docs for the startListening method from the ShakeDetector class, for the Dart programming language.">
  <title>startListening method - ShakeDetector class - shake library - Dart API</title>


  
  <link rel="preconnect" href="https://fonts.gstatic.com">
  <link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:ital,wght@0,300;0,400;0,500;0,700;1,400&display=swap" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@24,400,0,0" rel="stylesheet">
  
  <link rel="stylesheet" href="../../static-assets/github.css?v1">
  <link rel="stylesheet" href="../../static-assets/styles.css?v1">
  <link rel="icon" href="../../static-assets/favicon.png?v1">
  
</head>

<body data-base-href="../../" data-using-base-href="false" class="light-theme">
<div id="overlay-under-drawer"></div>
<header id="title">
  <span id="sidenav-left-toggle" class="material-symbols-outlined" role="button" tabindex="0">menu</span>
  <ol class="breadcrumbs gt-separated dark hidden-xs">
    <li><a href="../../index.html">shake</a></li>
    <li><a href="../../shake/shake-library.html">shake.dart</a></li>
    <li><a href="../../shake/ShakeDetector-class.html">ShakeDetector</a></li>
    <li class="self-crumb">startListening method</li>
  </ol>
  <div class="self-name">startListening</div>
  <form class="search navbar-right" role="search">
    <input type="text" id="search-box" autocomplete="off" disabled class="form-control typeahead" placeholder="Loading search...">
  </form>
  <div class="toggle" id="theme-button" title="Toggle brightness">
    <label for="theme">
      <input type="checkbox" id="theme" value="light-theme">
      <span id="dark-theme-button" class="material-symbols-outlined">
        dark_mode
      </span>
      <span id="light-theme-button" class="material-symbols-outlined">
        light_mode
      </span>
    </label>
  </div>
</header>
<main>
<div
    id="dartdoc-main-content"
    class="main-content"
    data-above-sidebar="shake&#47;ShakeDetector-class-sidebar.html"
    data-below-sidebar="">
    <div>
<h1><span class="kind-method">startListening</span> method 
</h1></div>

    <section class="multi-line-signature">
      
<span class="returntype">void</span>
<span class="name ">startListening</span>(<wbr>)

      

    </section>
    
<section class="desc markdown">
  <p>Starts listening to accelerometer events</p>
</section>


    
<section class="summary source-code" id="source">
  <h2><span>Implementation</span></h2>
  <pre class="language-dart"><code class="language-dart">void startListening() {
  streamSubscription = accelerometerEventStream().listen(
    (AccelerometerEvent event) {
      final double x = event.x;
      final double y = event.y;
      final double z = event.z;

      final double gX = x &#47; 9.80665;
      final double gY = y &#47; 9.80665;
      final double gZ = z &#47; 9.80665;

      &#47;&#47; gForce will be close to 1 when there is no movement.
      double gForce = sqrt(gX * gX + gY * gY + gZ * gZ);

      if (useFilter) {
        &#47;&#47; Apply a simple moving average filter
        if (_filterBuffer.length &gt;= _filterSamples) {
          _filterBuffer.removeAt(0);
        }
        _filterBuffer.add(gForce);

        &#47;&#47; Calculate the average
        gForce = _filterBuffer.reduce((a, b) =&gt; a + b) &#47; _filterBuffer.length;
      }

      if (gForce &gt; shakeThresholdGravity) {
        final int now = DateTime.now().millisecondsSinceEpoch;

        &#47;&#47; ignore shake events too close to each other (500ms)
        if (_shakeTimestamp + shakeSlopTimeMS &gt; now) {
          return;
        }

        &#47;&#47; reset the shake count after 3 seconds of no shakes
        if (_shakeTimestamp + shakeCountResetTime &lt; now) {
          _shakeCount = 0;
        }

        _shakeTimestamp = now;
        _shakeCount++;

        if (_shakeCount &gt;= minimumShakeCount) {
          &#47;&#47; Determine the primary direction of the shake
          ShakeDirection direction;
          final double absX = gX.abs();
          final double absY = gY.abs();
          final double absZ = gZ.abs();

          if (absX &gt; absY &amp;&amp; absX &gt; absZ) {
            direction = ShakeDirection.x;
          } else if (absY &gt; absX &amp;&amp; absY &gt; absZ) {
            direction = ShakeDirection.y;
          } else if (absZ &gt; absX &amp;&amp; absZ &gt; absY) {
            direction = ShakeDirection.z;
          } else {
            direction = ShakeDirection.undefined;
          }

          &#47;&#47; Create a shake event
          final ShakeEvent shakeEvent = ShakeEvent(
            direction: direction,
            force: gForce,
            timestamp: DateTime.fromMillisecondsSinceEpoch(now),
          );

          &#47;&#47; Call the main callback with shake event information
          onPhoneShake(shakeEvent);
        }
      }
    },
  );
}</code></pre>
</section>


  </div> <!-- /.main-content -->
  <div id="dartdoc-sidebar-left" class="sidebar sidebar-offcanvas-left">
    <!-- The search input and breadcrumbs below are only responsively visible at low resolutions. -->
<header id="header-search-sidebar" class="hidden-l">
  <form class="search-sidebar" role="search">
    <input type="text" id="search-sidebar" autocomplete="off" disabled class="form-control typeahead" placeholder="Loading search...">
  </form>
</header>
<ol class="breadcrumbs gt-separated dark hidden-l" id="sidebar-nav">
    <li><a href="../../index.html">shake</a></li>
    <li><a href="../../shake/shake-library.html">shake</a></li>
    <li><a href="../../shake/ShakeDetector-class.html">ShakeDetector</a></li>
    <li class="self-crumb">startListening method</li>
</ol>

    <h5>ShakeDetector class</h5>
    <div id="dartdoc-sidebar-left-content"></div>
  </div><!--/.sidebar-offcanvas-->
  <div id="dartdoc-sidebar-right" class="sidebar sidebar-offcanvas-right">
</div><!--/.sidebar-offcanvas-->
</main>
<footer>
  <span class="no-break">
    shake
      3.0.0
  </span>
  
</footer>


<script src="../../static-assets/highlight.pack.js?v1"></script>
<script src="../../static-assets/docs.dart.js"></script>

</body>
</html>

